<?php
class qcatController extends AController{	
	private $tableName='CATEGORY';
	
	function indexAction() {
		$select = JYFilterUtils::unfilterText($this->request->getGetParameter('s'));
		$page = $this->request->getGetParameter('p',1);
		$pagesize = $this->request->getGetParameter('ps',10);
		$modId = JYFilterUtils::unfilterText($this->request->getGetParameter('t'));
		if(empty($modId)){
			$this->alert('参数不对');
		}
		$totalNum = $this->getCateTolNumById($modId,$select);
		$this->totalNum = $totalNum[0]['NUM'];
//		var_dump($allCate);
		$this->pageNum = ceil($this->totalNum/$pagesize);
		if($page > $this->pageNum || $page<1){
			$page = $this->pageNum;
		}
		$allCate = $this->getCategoryById($modId,$page,$pagesize,$select);
		$this->allCate = $allCate;
		$this->page = $page;
		$this->pagesize = $pagesize;
		$this->select = $select;
		$this->mid = $modId;
		return $this->render();
	}
	
	function addAction() {
		if(isset($_POST['cate_add'])){
			$cate['cat_name'] = $this->request->getPostParameter('catename');
			if(!empty($cate['cat_name'])){
				$cate['cat_type'] = $mid = $this->request->getPostParameter('mid');
				$select = $this->request->getPostParameter('sel');
				$page = $this->request->getPostParameter('page');
				$cate['state']=1;
				$cate['unit_id']=$this->unid;
				$cate['upd_user']=$this->username;
				$cate['upd_time']=date('Y-m-d H:i:s');
				if($this->addCateInfo($cate)){
					$this->alert('添加成功',"/index.php?c=qcat&a=index&t=$mid&s=$select&p=$page");
					exit;
				}
				$this->alert('参数值错误');
				exit;
			}
		}
		
		$select = JYFilterUtils::unfilterText($this->request->getGetParameter('s'));
		$page = JYFilterUtils::unfilterText($this->request->getGetParameter('p'));
		$mid = JYFilterUtils::unfilterText($this->request->getGetParameter('t'));
		$this->mid = $mid;
		$this->sel = $select;
		$this->page = $page;
		return $this->render();
	}
	function editAction() {
		if(isset($_POST['cate_sub'])){
			$select = $this->request->getPostParameter('sel');
			$mid = $this->request->getPostParameter('mid');
			$id = $this->request->getPostParameter('cid');
			$page = $this->request->getPostParameter('page');
			$cate['cat_name'] = $this->request->getPostParameter('catename');
			if(!empty($cate['cat_name'])){
				$cate['upd_user'] = $this->username;
				$cate['upd_time']=date('Y-m-d H:i:s');
				if($this->updCateInfo($id,$cate)){
					$this->alert('修改成功',"/index.php?c=qcat&a=index&t=$mid&s=$select&p=$page");
					exit;
				}
			}
			$this->alert('参数值错误');
			exit;
		}
		$cid = JYFilterUtils::unfilterText($this->request->getGetParameter('id'));
		$select = JYFilterUtils::unfilterText($this->request->getGetParameter('s'));
		$page = JYFilterUtils::unfilterText($this->request->getGetParameter('p'));
		$mid = JYFilterUtils::unfilterText($this->request->getGetParameter('t'));
		$cateCon = $this->getCateInfoById($cid);
		$this->cateInfo = $cateCon;
		$this->mid = $mid;
		$this->sel = $select;
		$this->page = $page;
		return $this->render();
//		var_dump($cateCon);
	}
	function qcat_headAction() {
		if(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
			$callback = $_SERVER['HTTP_REFERER'];
			$url = parse_url($callback);
			$url = explode('&',$url['query']);
			foreach ($url as $v){
				$ret = explode('=', $v);
				$query[$ret[0]] = $ret[1];
			}
			unset($url);
			unset($ret);
//			var_dump($query);
			if($query['c']=='qcat'){
				switch ($query['t']) {
					case '1':$cate='问卷分类管理';break;
					case '2':$cate='题目分类管理';break;
					default:$cate='分类管理';
				}
			}
			switch ($query['c']){
				case 'qcat':$head="<div class='m_positin' style='cursor:pointer;' onclick='document.location.href=\"/index.php?c=".$query['c']."&a=index&t=".$query['t']."\"'>".$cate."&nbsp;</div>";break;
				case 'quesnr':$head="<div class='m_positin' style='cursor:pointer;' onclick='document.location.href=\"/index.php?c=".$query['c']."&a=index\"'>问卷管理&nbsp;</div>";break;
				case 'feed':$head="<div class='m_positin' style='cursor:pointer;' onclick='document.location.href=\"/index.php?c=".$query['c']."&a=index\"'>咨询反馈&nbsp;</div>";break;
				case 'questmanage':$head="<div class='m_positin' style='cursor:pointer;' onclick='document.location.href=\"/index.php?c=quest&a=index\"'>题库管理&nbsp;</div>"
								."<div class='m_positin' style='cursor:pointer;' onclick='document.location.href=\"/index.php?c=".$query['c']."&a=index&id=".$query['id']."\"'>题库选项管理&nbsp;</div>";break;
				case 'quest':$head="<div class='m_positin' style='cursor:pointer;' onclick='document.location.href=\"/index.php?c=".$query['c']."&a=index\"'>题库管理&nbsp;</div>";break;
				case 'user':$head="<div class='m_positin' style='cursor:pointer;' onclick='document.location.href=\"/index.php?c=".$query['c']."&a=index\"'>用户管理&nbsp;</div>";break;
				case 'visitrule':$head="<div class='m_positin' style='cursor:pointer;' onclick='document.location.href=\"/index.php?c=".$query['c']."&a=index\"'>回访规则设置&nbsp;</div>";break;
				case 'role':$head="<div class='m_positin' style='cursor:pointer;' onclick='document.location.href=\"/index.php?c=".$query['c']."&a=index\"'>权限组设置&nbsp;</div>";break;
				case 'recordmsg':$head="<div class='m_positin' style='cursor:pointer;' onclick='document.location.href=\"/index.php?c=".$query['c']."&a=".$query['a']."\"'>查询统计&nbsp;</div>";break;
				case 'stats':$head="<div class='m_positin' style='cursor:pointer;' onclick='document.location.href=\"/index.php?c=".$query['c']."&a=".$query['a']."\"'>决策分析&nbsp;</div>";break;
				case 'visitzy':$head="<div class='m_positin' style='cursor:pointer;' onclick='document.location.href=\"/index.php?c=".$query['c']."&a=".$query['a']."\"'>住院回访&nbsp;</div>";break;
				case 'patient':$head="<div class='m_positin' style='cursor:pointer;' onclick='document.location.href=\"/index.php?c=".$query['c']."&a=".$query['a']."\"'>客户档案管理&nbsp;</div>";break;
				case 'visit':$head="<div class='m_positin' style='cursor:pointer;' onclick='document.location.href=\"/index.php?c=".$query['c']."&a=".$query['a']."\"'>回访管理&nbsp;</div>";break;
				default:$head="";break;
			}
			switch ($query['a']){
				case 'addstep2':$head.="<div class='m_positin'>添加&nbsp;</div><div class='m_positin'>添加题目&nbsp;</div>";break;
				case 'add':$head.="<div class='m_positin'>添加/编辑</div>";break;
				case 'edit':$head.="<div class='m_positin'>编辑&nbsp;</div>";break;
				case 'showquest':$head.="<div class='m_positin'>问卷题目预览&nbsp;</div>";break;
				case 'zixunList':$head.="<div class='m_positin'>咨询信息回复&nbsp;</div>";break;
				case 'feedback':$head.="<div class='m_positin'>反馈跟踪&nbsp;</div>";break;
				case 'lookfeedback':$head.="<div class='m_positin'>反馈跟踪查看&nbsp;</div>";break;
				case 'resetpwd':$head.="<div class='m_positin'>用户密码重置&nbsp;</div>";break;
				case 'msg':$head.="<div class='m_positin'>短信查询&nbsp;</div>";break;
				case 'record':$head.="<div class='m_positin'>录音查询&nbsp;</div>";break;
				case 'msg_watch':$head.="<div class='m_positin'>短信详情&nbsp;</div>";break;
				case 'quesnr_stats':$head.="<div class='m_positin'>问卷统计&nbsp;</div>";break;
				default:$head.="";break;
			}
			$monthAccess = $this->getUserAccessNums('month');
			$dayAccess = $this->getUserAccessNums('day');
			$sucAccess = $this->getUserAccessNums('suc');
			$againAccess = $this->getUserAccessNums('again');
			$failAccess = $dayAccess - $sucAccess - $againAccess;
			$this->head = $head;
			$this->monthAccess = isset($monthAccess)?$monthAccess:0;
			$this->dayAccess = isset($dayAccess)?$dayAccess:0;
			$this->sucAccess = $sucAccess;
			$this->againAccess = $againAccess;
			$this->failAccess = $failAccess;
			return $this->render();
		}
	}
	function delAction() {
		$cid = JYFilterUtils::unfilterText($this->request->getGetParameter('id'));
		if($this->delCategoryById($cid)){
			$this->alert('删除成功');
		}
	}
	function getCategoryById($id,$page=0,$pagesize,$select=''){
//		$key = md5(__FUNCTION__.$id.$page.$pagesize.$select);
//		if(JYMemcache::get($key)){
//			return unserialize(JYMemcache::get($key));
//		}
		if($this->unid<0){
			$where = 'state=1 and cat_type='.$id;
		}else{
			$where = 'state=1 and unit_id='.$this->unid.' and cat_type='.$id;
		}
		$fields = 'cat_id,cat_name,upd_time,upd_user';
		$order = 'upd_time desc';
		if(!empty($select)){
			$where .= ' and cat_name like \'%'.$select.'%\'';
		}
		$data = JYDB::findAll($this->tableName,$where,$fields,$order,$page,$pagesize);
//		JYMemcache::set($key,serialize($data),60);
		return $data;
	}
	function getCateTolNumById($id,$select = ''){
//		$key = md5(__FUNCTION__.$id.$select);
//		if(JYMemcache::get($key)){
//			return unserialize(JYMemcache::get($key));
//		}
		if($this->unid<0){
			$where = ' and state=1';
		}else{
			$where = ' and state=1 and unit_id='.$this->unid;
		}
		if(!empty($select)){
			$where .= ' and cat_name like \'%'.$select.'%\'';
		}
		$sql = 'select count(*) num from CATEGORY where cat_type='.$id.$where;
		$data = JYDB::query($sql);
//		JYMemcache::set($key,serialize($data),3600*24);
		return $data;
	}
	function getUserAccessNums($type){
		switch ($type) {
			case 'day': $data = JYDB::find('visit','user_id = '.CSM_USER_ID.' and visit_time >= \''.date('Y-m-d').'\'','count(*) nums');break;
			case 'month':$data = JYDB::find('visit','user_id = '.CSM_USER_ID.' and visit_time > \''.date('Y-m-01').'\'','count(*) nums');break;
			case 'suc':$data = JYDB::find('visit','user_id = '.CSM_USER_ID.' and visit_state=\'B\' and visit_time >= \''.date('Y-m-d').'\'','count(*) nums');break;
			case 'again':$data = JYDB::find('visit','user_id = '.CSM_USER_ID.' and visit_state in (\'C\',\'H\',\'I\') and visit_time >= \''.date('Y-m-d').'\'','count(*) nums');break;
		}
		return $data['NUMS'];
	}
	function getCateInfoById($id){
		if($this->unid<0){
			$where = 'state=1 and cat_id ='.$id;
		}else{
			$where = 'state=1 and unit_id='.$this->unid.' and cat_id ='.$id;
		}
		
		return JYDB::find($this->tableName,$where);
	}
	function addCateInfo($arr){
		return JYDB::create($this->tableName,$arr,'CAT_ID');
	}
	function updCateInfo($id,$arr){
		$where = 'cat_id='.$id;
		return JYDB::update($this->tableName,$where,$arr);
	}
	function delCategoryById($id){
		$where = 'cat_id='.$id;
		return JYDB::update($this->tableName,$where,array('state'=>0));
	}
}